/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import cn.ac.iie.di.ban.ycl.idgen.IDGeneratorInterface;
import cn.ac.iie.di.ban.ycl.idgen.IDSHAGeneratorImpl;
import cn.ac.iie.di.msg.JsonMsgSender;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

/**
 *
 * @author lenovo-pc
 */
public class SplitJsonArray {

    static private final Logger logger = Logger.getLogger(SplitJsonArray.class.getName());

    public static void main(String[] args) throws FileNotFoundException, IOException {
        String sJson = "{\"record_meta\":[{\"name\":\"message_id\",\"type\":\"string\"},{\"name\":\"url\",\"type\":\"string\"},{\"name\":\"content\",\"type\":\"string\"},{\"name\":\"user_id\",\"type\":\"string\"},{\"name\":\"is_retweet\",\"type\":\"int\"},{\"name\":\"origin_user_name\",\"type\":\"string\"},{\"name\":\"origin_message_id\",\"type\":\"string\"},{\"name\":\"create_time\",\"type\":\"string\"},{\"name\":\"crawl_time\",\"type\":\"timestamp\"},{\"name\":\"favorited_count\",\"type\":\"int64\"},{\"name\":\"retweet_count\",\"type\":\"int64\"},{\"name\":\"user_name\",\"type\":\"string\"},{\"name\":\"reply_to_screen_name\",\"type\":\"string\"},{\"name\":\"mention_users\",\"type\":\"string\"},{\"name\":\"location\",\"type\":\"string\"}],\"record_list\":[{\"message_id\":\"805476342150164480\",\"url\":\"https://twitter.com/gonzales7661/status/805476342150164480\",\"content\":\"People will question your why. At times, you will question your why. Just never forget WHY you started!\",\"user_id\":\"61070071\",\"is_retweet\":\"true\",\"origin_user_name\":\"CoachSalgeHCC\",\"origin_message_id\":\"805235445227470850\",\"create_time\":\"2016-12-04 02:21:05\",\"crawl_time\":\"2016-12-25 07:40:31.0\",\"favorited_count\":\"1\",\"retweet_count\":\"0\",\"user_name\":\"gonzales7661\",\"reply_to_screen_name\":\"null\",\"mention_users\":\"\",\"location\":\"null\"}]}";
        StringBuffer sb = new StringBuffer("");
        new SplitJsonArray().paserContent(sJson);
        FileReader reader = new FileReader("D://test_imsg");
        BufferedReader br = new BufferedReader(reader);

        String str = null;

        while ((str = br.readLine()) != null) {
            sb.append(str + "/n");

            System.out.println(str);
            new SplitJsonArray().paserContent(str);
        }

        br.close();
        reader.close();

    }

    private ArrayList<String> message_id = new ArrayList<>();
    private ArrayList<String> create_time = new ArrayList<>();
    private ArrayList<String> crawl_time = new ArrayList<>();
    private ArrayList<String> url = new ArrayList<>();
    private ArrayList<String> Message_type = new ArrayList<>();
    private ArrayList<String> content = new ArrayList<>();
    private ArrayList<String> user_id = new ArrayList<>();
    private ArrayList<String> user_name = new ArrayList<>();
    private ArrayList<String> comment_cnt = new ArrayList<>();
    private ArrayList<String> share_cnt = new ArrayList<>();
    private ArrayList<String> like_cnt = new ArrayList<>();
    private int[] idDuplicate = null;
    private int[] cententDuplicate = null;

    private int count = 1;
    private JSONObject jsonString = null;

    public void paserContent(String str) {
        try {
            IDGeneratorInterface iDGeneratorInterface = new IDSHAGeneratorImpl();
            message_id.clear();
            create_time.clear();
            crawl_time.clear();
            url.clear();
            Message_type.clear();
            content.clear();
            user_id.clear();
            user_name.clear();
            comment_cnt.clear();
            share_cnt.clear();
            like_cnt.clear();
            jsonString = JSONObject.fromObject(str);
//		System.out.println(jsonString.get("WxId"));
//		System.out.println(jsonString.get("SendTime"));
            JSONArray jaArray = jsonString.getJSONArray("record_list");
            count = jaArray.size();
            for (int i = 0; i < jaArray.size(); i++) {
                JSONObject jo = (JSONObject) jaArray.get(i);

                message_id.add(jo.get("message_id").toString());
                create_time.add(jo.get("create_time").toString());
                crawl_time.add(jo.get("crawl_time").toString());
                url.add(jo.get("url").toString());
                Message_type.add(jo.get("Message_type").toString());
                content.add(jo.get("content").toString());
                user_id.add(jo.get("user_id").toString());
                user_name.add(jo.get("user_name").toString());
                comment_cnt.add(jo.get("comment_cnt").toString());
                share_cnt.add(jo.get("share_cnt").toString());
                like_cnt.add(jo.get("like_cnt").toString());

//                System.out.println("哈哈" + Message_type.get(0));
//                System.out.println("哈哈" + Message_type.toString());
            }
            try {
                String idString = "";
                for (int i = 0; i < message_id.size(); i++) {
                    idString = idString + GenerateMD5.string2MD5(iDGeneratorInterface.getId(String.valueOf(7), "M-FBOOK01", "facebook", "facebook", message_id.get(i))) + "\n";
                }
                idDuplicate = RemoveDuplicate.getQCId(idString);
            } catch (Exception ex) {
                logger.error("know idDuplicate Task error " + ex, ex);
            }
            try {
                String contentString = "";
                for (int i = 0; i < content.size(); i++) {
                    contentString = contentString + content.get(i) + "\n";
                }
                cententDuplicate = RemoveDuplicate.getQCId(contentString);
            } catch (Exception ex) {
                logger.error("know cententDuplicate Task error " + ex, ex);
            }

        } catch (Exception e) {
            logger.error(e, e);
        }

    }

    public ArrayList<String> getMessage_id() {
        return message_id;
    }

    public ArrayList<String> getCreate_time() {
        return create_time;
    }

    public ArrayList<String> getCrawl_time() {
        return crawl_time;
    }

    public ArrayList<String> getUrl() {
        return url;
    }

    public ArrayList<String> getMessage_type() {
        return Message_type;
    }

    public ArrayList<String> getContent() {
        return content;
    }

    public ArrayList<String> getUser_id() {
        return user_id;
    }

    public ArrayList<String> getUser_name() {
        return user_name;
    }

    public ArrayList<String> getComment_cnt() {
        return comment_cnt;
    }

    public ArrayList<String> getShare_cnt() {
        return share_cnt;
    }

    public ArrayList<String> getLike_cnt() {
        return like_cnt;
    }

    public int[] getIdDuplicate() {
        return idDuplicate;
    }

    public int[] getCententDuplicate() {
        return cententDuplicate;
    }

    /**
     * @return the count
     */
    public int getCount() {
        return count;
    }

    public JSONObject getJsonString() {
        return jsonString;
    }
}
